import 'package:flutter/material.dart';

/// 更新ListView
class UpdateListViewApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.blue),
      home: UpdateListViewAppPage(),
    );
  }
}

class UpdateListViewAppPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => UpdateListViewAppPageState();
}

class UpdateListViewAppPageState extends State<UpdateListViewAppPage> {
  List<Widget> widgets = [];

  @override
  void initState() {
    super.initState();
    for (int i = 0; i < 100; i++) {
      widgets.add(getRow(i));
    }
  }

//  @override
//  Widget build(BuildContext context) {
//    return Scaffold(
//      appBar: AppBar(
//        title: Text("更新 ListView"),
//      ),
//      body: ListView(
//        children: widgets,
//      ),
//    );
//  }
//
//  Widget getRow(int i) {
//    return GestureDetector(
//        child: Padding(
//          padding: EdgeInsets.all(10.0),
//          child: Text("Row $i"),
//        ),
//        onTap: () {
//          setState(() {
//            widgets = List.from(widgets);
//            widgets.add(getRow(widgets.length + 1));
//            print('row $i');
//          });
//        });
//  }

  /// ============================================================================
  /// ==================================Recommend=================================
  /// ============================================================================

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("更新 ListView"),
      ),
      body: ListView.builder(
        itemCount: widgets.length,
        itemBuilder: (BuildContext context, int position) {
          return getRow(position);
        },
      ),
    );
  }

  Widget getRow(int i) {
    return GestureDetector(
      child: Padding(
        padding: EdgeInsets.all(10.0),
        child: Text("Row $i"),
      ),
      onTap: () {
        setState(() {
          widgets.add(getRow(widgets.length + 1));
          print("row $i");
        });
      },
    );
  }
}
